package com.sunday.scoremanager.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.Constants;
import com.sunday.common.utils.SharePerferenceUtils;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.utils.TimeCount;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.scoremanager.R;
import com.sunday.scoremanager.base.BaseApp;
import com.sunday.scoremanager.entity.Member;
import com.sunday.scoremanager.http.ApiClient;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.sunday.scoremanager.base.Constants.CODE_1;
import static com.sunday.scoremanager.base.Constants.SEND_CODE_SIGN;

/**
 * Created by 刘涛 on 2017/10/30.
 */

public class BossLoginActivity extends BaseActivity {


    @Bind(R.id.left_btn)
    ImageView leftBtn;
    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.rightTxt)
    TextView rightTxt;
    @Bind(R.id.right_btn)
    ImageView rightBtn;
    @Bind(R.id.common_header)
    RelativeLayout commonHeader;
    @Bind(R.id.edit_mobile)
    ClearEditText editMobile;
    @Bind(R.id.edit_code)
    ClearEditText editCode;
    @Bind(R.id.send_code)
    TextView sendCode;
    @Bind(R.id.btn_login)
    TextView btnLogin;
    private TimeCount timeCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_boss_login);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        titleView.setText("Boss登录");
        titleView.setTextColor(ContextCompat.getColor(mContext, R.color.main_color));
        timeCount = new TimeCount(60000, 1000, sendCode);
    }

    private String mobile, code;

    @OnClick({R.id.send_code, R.id.btn_login})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.send_code:
                mobile = editMobile.getText().toString().trim();
                if (TextUtils.isEmpty(mobile) || !StringUtils.isMobileNO(mobile)) {
                    ToastUtils.showToast(mContext, "请输入正确的手机号");
                    return;
                }
                sendCode(mobile);
                break;
            case R.id.btn_login:
                mobile = editMobile.getText().toString().trim();
                if (TextUtils.isEmpty(mobile) || !StringUtils.isMobileNO(mobile)) {
                    ToastUtils.showToast(mContext, "请输入正确的手机号");
                    return;
                }
                code = editCode.getText().toString().trim();
                if (TextUtils.isEmpty(code)) {
                    ToastUtils.showToast(mContext, "请输入验证码");
                    return;
                }

                bossLogin();
                break;
        }
    }

    private void sendCode(String mobile) {
        showLoadingDialog(0);
        Call<ResultDO> call = ApiClient.getApiAdapter().sendCode(mobile, SEND_CODE_SIGN, CODE_1, 5);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                dissMissDialog();
                if (isFinish || response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    ToastUtils.showToast(mContext, "验证码已发送到手机");
                    timeCount.start();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }

    private void bossLogin() {
        showLoadingDialog(0);
        Call<ResultDO<List<Member>>> call = ApiClient.getApiAdapter().bossLogin(mobile, code);
        call.enqueue(new Callback<ResultDO<List<Member>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<Member>>> call, Response<ResultDO<List<Member>>> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                dissMissDialog();
                ResultDO<List<Member>> resultDO = response.body();
                if (response.body().getCode() == 0) {
//                    SharePerferenceUtils.getIns(mContext).saveOAuth(resultDO.getResult());
//                    SharePerferenceUtils.getIns(mContext).putBoolean(Constants.IS_LOGIN, true);
//                    List<Member> member = response.body().getResult();
//                    BaseApp.getInstance().setMember(member);
                    if (response.body().getResult() == null) {
                        return;
                    }
                    intent = new Intent(mContext, ShopListActivity.class);
                    intent.putParcelableArrayListExtra("memberList", (ArrayList<Member>) (response.body().getResult()));
                    startActivity(intent);
                    finish();
                } else {
                    ToastUtils.showToast(mContext, resultDO.getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<Member>>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timeCount != null) {
            timeCount.cancel();
        }
    }
}
